# !/usr/bin/env python
# encoding: utf-8
'''
  @author: HJW
  @license: (C) Copyright 2013-2017, Node Supply Chain Manager Corporation Limited.
  @contact: hejunwang01@126.com
  @file: project_list.py
  @time: 2022/9/11 0011 下午 21:56
  @desc:
  '''


from flask import Blueprint,request,jsonify
import logging
from dbs import db
from models import ProTestCase
from utils.operadirectory import as_dict


# 注册的蓝图 ,url_prefix 在所有的路径前面增加/project
bp = Blueprint('project',__name__,url_prefix='/project')
logger = logging.getLogger(__name__)

TESTSCRIPT ="/root/flask-celery-hjw/flask-celery/pytest_pkg/testcase-data/"


# 获取所有创建的测试集合
@bp.route('/pro/allpro',methods=['GET'])
def allpro():
    pro_result=ProTestCase.query.order_by(-ProTestCase.id).limit(10).all()
    if pro_result:
        data_list = []
        for item in pro_result:
            data_list.append(as_dict(item))
        return  jsonify({'code': 200, "msg": "查询成功!","data":data_list})
    else:
        return  jsonify({'code': -1, "msg": "查询失败!"})


@bp.route('/pro/addpro',methods=['POST'])
def addpro():
    if request.method =="POST":
        name = request.json.get('name')
        print(name)
        query = ProTestCase.query.filter_by(pro_name=name).all()
        if len(query):
            return jsonify({"code": -1, "message": "该用户名已经存在!"})
        else:
            try:
                proadd = ProTestCase(pro_name = name)
                # session记录对象任务
                db.session.add(proadd)
                # 提交任务到数据库中
                db.session.commit()
                return jsonify({"code": 200, "message": "添加成功!"})
            except Exception as e :
                db.session.rollback()  # 事务回滚
                return jsonify({"code": -1, "message": e})

    else:

        return jsonify({"code":-1,"msg":"请求方式不对"})





